/**
 * @BelongsProject : LeetCode
 * @Author : WXY
 * @CreateTime : 2022-12-08  18:44
 * @Description :剑指 Offer 25. 合并两个排序的链表
 * @Version : 1.0
 */

public class Num025_mergeTwoLists {
    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null)return l2;
        if (l2 == null)return l1;
        ListNode cur = l1.val > l2.val ? l2 : l1;
        if (cur == l1) {
            l1 = l1.next;
        } else {
            l2 = l2.next;
        }
        cur.next = null;
        ListNode head = cur;
        while (l1 != null && l2 != null) {
            //cur.next = l1.val > l2.val ? l2 : l1;
            if (l1.val > l2.val) {
                cur.next = l2;
                l2 = l2.next;
            } else {
                cur.next = l1;
                l1 = l1.next;
            }
            cur = cur.next;
        }
        if (l1 != null) {
            cur.next = l1;
        }else {
            cur.next = l2;
        }
//        while (l1 != null) {
//            cur.next = l1;
//            l1 = l1.next;
//            cur = cur.next;
//        }
//        while (l2 != null) {
//            cur.next = l2;
//            l2 = l2.next;
//            cur = cur.next;
//        }
        return head;
    }
}
